﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;

namespace LenderClearingHouse
{
    public partial class MasterListEditControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.DataBinding +=new EventHandler(MasterListEditControl_DataBinding);
        }

        protected void FillBed()
        {
            edBedrooms.Items.Clear();
            for (int i = 1; i <= 20; i++)
            {
                edBedrooms.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }
            
        }

        protected void MasterListEditControl_DataBinding(object sender, EventArgs e)
        {
            FillBed();
        }

        protected void btnLookup_Click(object sender, EventArgs e)
        {
            lblLookupError.Visible = false;
            lblLookupMessage.Visible = false;

            if (string.IsNullOrEmpty(edAPN.Text))
            {
                //ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "Error", string.Format("alert('{0}');", "Apn can't be empty!"), true);
                lblLookupError.Visible = true;
                lblLookupError.Text = "Apn can't be empty!";
                return;
            }

            DataAccess da = new DataAccess();
            DataTable dt = da.GetDataTable("lookupMasterList", new string[] { "@Apn" }, new object[] { edAPN.Text });
            if (dt.Rows.Count == 0)
            {
                lblLookupError.Visible = true;
                lblLookupError.Text = "Apn not found!";
                return;
            }
            else
            {
                DataRow drv = dt.Rows[0];
                
                edTsNumber.Text = drv.IsNull("TsNumber") ? "" : drv["TsNumber"].ToString();
                edPropertyLocation.Text = drv.IsNull("PropertyLocation")?"":drv["PropertyLocation"].ToString();
                edZipCode.Text = drv.IsNull("ZipCode") ? "" : drv["ZipCode"].ToString();
                edStories.Text = drv.IsNull("Stories") ? "" : drv["Stories"].ToString();
                edTsOpeningBid.DbValue = drv["TsOpeningBid"];
                edNos.Text = drv.IsNull("Nos") ? "" : drv["Nos"].ToString();
                edReconTrust_DateOfSale.DbSelectedDate = drv["ReconTrust_DateOfSale"];
                edLPS_SalesStatus.Text = drv.IsNull("LPS_SalesStatus") ? "" : drv["LPS_SalesStatus"].ToString();
                edPriorityPost_Status.Text = drv.IsNull("PriorityPost_Status") ? "" : drv["PriorityPost_Status"].ToString();
                edNdsCorp_SaleDate.Text = drv.IsNull("NdsCorp_SaleDate") ? "" : drv["NdsCorp_SaleDate"].ToString();
                edHoa.Text = drv.IsNull("Hoa") ? "" : drv["Hoa"].ToString();
                Utilities.SetValueToCombo(edListingStatus, drv["ListingStatus"]);
                edClient.Text = drv.IsNull("Client") ? "" : drv["Client"].ToString();

                edAuctionDate.DbSelectedDate = drv["AuctionDate"];
                edvo.Text = drv.IsNull("vo") ? "" : drv["vo"].ToString();
                edConstructYear.Text = drv.IsNull("ConstructYear") ? "" : drv["ConstructYear"].ToString();
                edTotalSqft.Text = drv.IsNull("TotalSqft") ? "" : drv["TotalSqft"].ToString();
                edLotSize.Text = drv.IsNull("LotSize") ? "" : drv["LotSize"].ToString();
                Utilities.SetValueToCombo(edBedrooms, drv["Bedrooms"]);
                edBathrooms.Text = drv.IsNull("Bathrooms") ? "" : drv["Bathrooms"].ToString();
                edGarageSqft.Text = drv.IsNull("GarageSqft") ? "" : drv["GarageSqft"].ToString();
                Utilities.SetValueToCombo(edPool, drv["Pool"]);
                edEstimatedBid.DbValue = drv["EstimatedBid"];
                edEstVal.DbValue = drv["EstVal"];
                edTax.DbValue = drv["Tax"];
                edSite.Text = drv.IsNull("Site") ? "" : drv["Site"].ToString();
                edOnOff.Text = drv.IsNull("OnOff") ? "" : drv["OnOff"].ToString();

                edTrustee.Text = drv.IsNull("Trustee") ? "" : drv["Trustee"].ToString();
                Utilities.SetValueToCombo(edSpa, drv["Spa"]);
                edMaxBid.DbValue = drv["MaxBid"];
                edLiens.Text = drv.IsNull("Liens") ? "" : drv["Liens"].ToString();
                edMiscNotes.Text = drv.IsNull("MiscNotes") ? "" : drv["MiscNotes"].ToString();
                edAssessorDesc.Text = drv.IsNull("AssessorDesc") ? "" : drv["AssessorDesc"].ToString();
                edPrelistStatus.Text = drv.IsNull("PrelistStatus") ? "" : drv["PrelistStatus"].ToString();
                edPurchasePrice.DbValue = drv["PurchasePrice"];
                edRehabCosts.DbValue = drv["RehabCosts"];
                edLandUse.Text = drv.IsNull("LandUse") ? "" : drv["LandUse"].ToString();
                Utilities.SetValueToCombo(edPhase, drv["Phase"]);
                edCloseDate.DbSelectedDate = drv["CloseDate"];
                edSource.Text = drv.IsNull("Source") ? "" : drv["Source"].ToString();
                edDateRecorded.DbSelectedDate = drv["DateRecorded"];

                IsLookup = true;
                lblLookupMessage.Visible = true;
                lblLookupMessage.Text = "Apn found successfully!";
            }
        }

        object _dataItem = null;

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;

                FillBed();
                edListingStatus.SelectedIndex = 2;
                edBedrooms.SelectedIndex = 0;
                edPool.SelectedIndex = 0;
                edSpa.SelectedIndex = 0;
                edPhase.SelectedIndex = 0;
                if ((value != null) && !(value is Telerik.Web.UI.GridInsertionObject))
                {
                    DataRowView drv = (DataRowView)value;

                    Utilities.SetValueToCombo(edListingStatus, drv["ListingStatus"]);
                    Utilities.SetValueToCombo(edBedrooms, drv["Bedrooms"]);
                    Utilities.SetValueToCombo(edPool, drv["Pool"]);
                    Utilities.SetValueToCombo(edSpa, drv["Spa"]);
                    Utilities.SetValueToCombo(edPhase, drv["Phase"]);
                }

                IsLookup = false;
            }
        }

        public bool IsLookup
        {
            get
            {
                return (ViewState["IsLookup"].ToString() == "1");
            }
            set
            {
                ViewState["IsLookup"] = value?"1":"0";
            }
        }
    }
}